package com.li.sssp.pc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.sun.org.apache.xpath.internal.operations.Bool;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class OperateExcel {

	public static void readExcel() throws BiffException, IOException {
		// 创建一个list 用来存储读取的内容
		List list = new ArrayList();
		Workbook rwb = null;
		Cell cell = null;

		// 创建输入流
		InputStream stream = new FileInputStream("H:\\Work2017\\pc\\网址清洗-测试题4月7日.xls");

		// 获取Excel文件对象
		rwb = Workbook.getWorkbook(stream);
		// 获取文件的指定工作表 默认的第一个
		Sheet sheet = rwb.getSheet(0);
		// 行数(表头的目录不需要，从1开始)
		for (int i = 0; i < sheet.getRows(); i++) {
			// 创建一个数组 用来存储每一列的值
			String[] str = new String[sheet.getColumns()];
			// 列数
			for (int j = 0; j < sheet.getColumns(); j++) {
				// for(int i=0;i<sheet.get)
				// 获取第i行，第j列的值
				cell = sheet.getCell(j, i);
				str[j] = cell.getContents();
			}
			// 把刚获取的列存入list
			list.add(str);
		}

		for (int i = 0; i < list.size(); i++) {
			String[] str = (String[]) list.get(i);
			for (int j = 0; j < str.length; j++) {
				// System.out.println(str[j]);
			}
		}
		stream.close();
	}

	public static void writeExcel() {
		String[] title = { "编号", "产品名称", "产品价格", "产品数量", "生产日期", "产地", "是否出口" };
		try {
			// 获得开始时间
			long start = System.currentTimeMillis();
			// 输出的excel的路径
			String filePath = "H:\\Work2017\\pc\\网址清洗-测试题4月7日.xls";
			// 创建Excel工作薄
			File file = new File(filePath);
			Workbook book = Workbook.getWorkbook(file);
			WritableWorkbook wbook = Workbook.createWorkbook(file, book); // 根据book创建一个操作对象

			// if(CheckUrl("www.001.sh.cn")){
			WritableSheet sh = wbook.createSheet("官网", 1);// 得到一个工作对象
			Label label = new Label(0, 0, "222");
			Label label2=new Label(1,0,"2223");
			sh.addCell(label);
			sh.addCell(label2);
			
			WritableSheet sh2 = wbook.createSheet("2官网", 2);// 得到一个工作对象
			Label label3=new Label(0,1,"5555");
			sh2.addCell(label3);
			// }
			wbook.write();
			wbook.close();

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public static String CheckUrl(String strUrl) throws IOException {

		// 1 getURL返回
		String name = null;

		String url = "http://icp.chinaz.com/" + strUrl;
		Document doc = Jsoup.connect(url).get();

		Elements elements = doc.select("li[class=bg-gray clearfix]");
		if (elements.size() != 0) {

			String companyName = doc.select("li[class=bg-gray clearfix]").get(0).select("p").get(0).childNode(0)
					.outerHtml();
			String webName = doc.select("li[class=clearfix]").get(1).select("p").get(0).childNode(0).outerHtml();
			if (companyName.equals(webName)) {
				name = companyName;
			}
		}
		// Element element = doc.select("tbody[class=bg-gray clearfix]").get(0);
		// System.out.print(element);
		return name;
	}

}
